package org.example.service.impl;

import cn.hutool.core.bean.BeanUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.example.Contant.BusinessException;
import org.example.Contant.Result;
import org.example.dto.UserDto;
import org.example.entity.User;
import org.example.service.ChatService;
import org.example.util.JwtUtil;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class ChatServiceImpl implements ChatService {

    @Resource
    private RedisTemplate<String, UserDto> redisTemplate;

    @Override
    public Result<UserDto> getCurrentUser(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        if ("".equals(token)){
            throw new BusinessException("500","用户未登录");
        }
        String username = JwtUtil.parseToken(token);
        UserDto user = redisTemplate.opsForValue().get(username);
        if (BeanUtil.isEmpty(user)){
            throw new BusinessException("500","登录过期");
        }
        return Result.success("200",user);
    }
}
